
Promise.MyAllSettled = function(promises){
    var results = new Array(promises.length)
    var count = 0
    function add(result, index, resolve, status){
        results[index] = {
            'status' : status,
        }
        if(status === 'rejected'){
            results[index].reason = result
        }else{
            results[index].value = result
        }
        if(count === promises.length){
           resolve(results)
        }
    }
    return new Promise((resolve, reject) => {
        promises.forEach((p,index) => {
            if((typeof p === 'object' && typeof p !== null) || typeof p === 'function'){
                let then = p.then
                if(typeof then === 'function'){
                    then.call(p, value => {
                        count += 1
                        add(value, index, resolve, 'fulfilled')
                    }, reason => {
                        count += 1
                        add(reason, index, resolve, 'rejected' )
                    })
                }else{
                    count += 1
                    add(p,index, resolve, 'fulfilled')
                    
                }
            }else{
                count += 1
                add(p,index, resolve,'fulfilled')
            }
        });
   })
   
}


const p1 = Promise.reject(111)
const p2 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve(222)
    }, 1000)
})
const p3 = function fun(){
    return 333
}
Promise.MyAllSettled([p1, p2, p3]).then((value)=>{
    console.log("手写输出", value)
}).catch((reason) => {
    console.log("手写输出catch" , reason)
})

Promise.allSettled([p1, p2, p3]).then((value)=>{
    console.log("原生输出",value)
}).catch((reason) => {
    console.log("手写输出catch" , reason)
})





